package com.feiyuan.modules.sys.service.impl;

import com.feiyuan.modules.sys.vo.MenuVO;
import com.feiyuan.modules.sys.vo.RoleMenuVO;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feiyuan.common.utils.PageUtils;
import com.feiyuan.common.utils.Query;

import com.feiyuan.modules.sys.mapper.RoleMenuDao;
import com.feiyuan.modules.sys.entity.RoleMenuEntity;
import com.feiyuan.modules.sys.service.RoleMenuService;
import org.springframework.util.CollectionUtils;


@Service("roleMenuService")
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuDao, RoleMenuEntity> implements RoleMenuService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<RoleMenuEntity> page = this.page(
                new Query<RoleMenuEntity>().getPage(params),
                new QueryWrapper<RoleMenuEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveRoleMenu(Long roleId, List<Long> menuIds) {
        List<RoleMenuEntity> rm = new ArrayList<>();
        for (Long menuId : menuIds) {
            RoleMenuEntity roleMenu = new RoleMenuEntity();
            roleMenu.setRoleId(roleId);
            roleMenu.setMenuId(menuId);
            rm.add(roleMenu);
        }
        this.saveBatch(rm);
    }

    @Override
    public void updateRoleMenu(Long roleId, List<Long> menuIds) {
        List<RoleMenuEntity> roleMenuList = baseMapper.selectList(new QueryWrapper<RoleMenuEntity>().eq("role_id", roleId));
        if (!CollectionUtils.isEmpty(roleMenuList)) {
            // 先删除在保存即可
            baseMapper.delete(new QueryWrapper<RoleMenuEntity>().eq("role_id", roleId));
        }
        this.saveRoleMenu(roleId, menuIds);
    }

    @Override
    public void deleteRoleMenu(List<Long> roleIds) {
        for (Long roleId : roleIds) {
            baseMapper.delete(new QueryWrapper<RoleMenuEntity>().eq("role_id", roleId));
        }
    }

    @Override
    public List<RoleMenuVO> selectMenuList(String roleId) {
        List<RoleMenuVO> roleMenuVOS = baseMapper.selectMenuList(roleId);
        for (RoleMenuVO roleMenuVO : roleMenuVOS) {
            List<MenuVO> menuVOS = new ArrayList<>();
            MenuVO menuVO = new MenuVO();
            menuVO.setPermissionId(roleMenuVO.getMenuId());
            menuVO.setAction(roleMenuVO.getPerms());
            menuVOS.add(menuVO);
            roleMenuVO.setActionEntitySet(menuVOS);
        }
        return roleMenuVOS;
    }

    @Override
    public List<Long> selectByRoleId(Long id) {
//        List<RoleMenuEntity> roleMenus = baseMapper.selectList(new QueryWrapper<RoleMenuEntity>().eq("role_id", id));
        List<RoleMenuEntity> roleMenus = baseMapper.selectRoleMenu(id);
        List<Long> menuIds = new ArrayList<>();
        if (!CollectionUtils.isEmpty(roleMenus)) {
            for (RoleMenuEntity roleMenu : roleMenus) {
                menuIds.add(roleMenu.getMenuId());
            }
        }
        return menuIds;
    }

}